package steak.xteamy.VoiceChat;

import de.maxhenkel.voicechat.api.*;
import de.maxhenkel.voicechat.api.events.*;
import de.maxhenkel.voicechat.api.packets.StaticSoundPacket;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.entity.player.PlayerEntity;
import steak.xteamy.KillerGroup;
import steak.xteamy.Network.Packets;
import steak.xteamy.VoiceChat.Pop.Bug;

import java.util.*;

public class Plugin implements VoicechatPlugin {

    public static VoicechatServerApi voicechatServerApi;

    public static HashMap<String, Group> groups = new HashMap<>();

    public record VoiceOption(float frequency, int amplitude, float randomFactor, int addon) {}
    public static VoiceOption VOICE_OPTION = new VoiceOption(0.8f, 80, 5.3f, -80); //之前的调试，已删

    @Override
    public String getPluginId() {
        return KillerGroup.MOD_ID;
    }

    @Override
    public void initialize(VoicechatApi api) {
        CommandRegistrationCallback.EVENT.register(KillerGroup.IdentifierMod("command"), Command.Init());
        Packets.registerPackets();
    }

    @Override
    public void registerEvents(EventRegistration registration) {
        registration.registerEvent(VoicechatServerStartedEvent.class, this::onServerStarted);
        registration.registerEvent(StaticSoundPacketEvent.class, this::onGroupSound);
    }

    private void onServerStarted(VoicechatServerStartedEvent event) {
        voicechatServerApi = event.getVoicechat();
        GroupManager.getInstance().initialize(voicechatServerApi);
    }

    private void onGroupSound(StaticSoundPacketEvent event) {
        VoicechatConnection senderConnection = event.getSenderConnection();

        if (senderConnection != null) {
            if (senderConnection.getGroup() != null) {

                UUID senderGroupId = senderConnection.getGroup().getId();
                boolean bl1 = senderGroupId.equals(GroupManager.WOLF_GROUP.getId());

                //死亡听取狼人频道
                for (UUID uuid : GroupManager.spectatorPlayers) {
                    VoicechatConnection receiverConnection = voicechatServerApi.getConnectionOf(uuid);

                    if (receiverConnection != null && receiverConnection.getGroup() != null) {
                        UUID receiverGroupId = receiverConnection.getGroup().getId();
                        boolean bl2 = receiverGroupId.equals(GroupManager.DEATH_GROUP.getId());
                        if (bl1 && bl2) {
                            StaticSoundPacket packet = sendPackets(event.getPacket(), false);

                            voicechatServerApi.sendStaticSoundPacketTo(receiverConnection, packet);
                        }
                    }
                }

                //窃听器
                long currentTime = System.currentTimeMillis();
                Iterator<Map.Entry<PlayerEntity, Long>> iterator = Bug.BUGS.entrySet().iterator();

                while (iterator.hasNext()) {
                    Map.Entry<PlayerEntity, Long> map = iterator.next();
                    long value = map.getValue();

                    if (value <= currentTime) {
                        iterator.remove();
                    } else {
                        VoicechatConnection receiverConnection = voicechatServerApi.getConnectionOf(map.getKey().getUuid());

                        if (receiverConnection != null) {
                            StaticSoundPacket packet = sendPackets(event.getPacket(), true);
                            voicechatServerApi.sendStaticSoundPacketTo(receiverConnection, packet);
                        }
                    }
                }
            }
        }
    }

    private StaticSoundPacket sendPackets(StaticSoundPacket packet, boolean anonymize) {
        if (anonymize) {
            packet = packet.staticSoundPacketBuilder()
                    .channelId(packet.getChannelId())
                    .category(packet.getCategory())
                    .opusEncodedData(AudioAnonymizer.audioProcessor(packet.getOpusEncodedData()))
                    .build();
        }
        return packet;
    }
}
